package controllers

import (
	"github.com/astaxie/beego"
	_ "github.com/go-sql-driver/mysql"
	"html/template"
	"log"
	"path"
	"src/gitee.com/wx_e6924dbcd0/notebook/models"
	"strconv"
	"strings"
)

type UserMainController struct {
	beego.Controller
}

func (c *UserMainController) Get() {
	status := c.GetString("status")
	id := c.GetString("id")
	if status == "1" {
		c.Ctx.SetCookie("upuser", id, 3)
		c.TplName = "up_user.html"
		return
	}
	c.TplName = "register.html"
	//
}

//注册用户
//http://127.0.0.1:8080/
func (c *UserMainController) DoRegister() {
	//c.TplName = "user_tb.html"
	//return
	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 插入数据到数据库
	name := template.HTMLEscapeString(c.Input().Get("Username"))
	sex := template.HTMLEscapeString(c.Input().Get("Usex"))
	passwd := template.HTMLEscapeString(c.Input().Get("Userpasswd"))
	// 获得图片对象
	f, h, err := c.GetFile("InputImgFile")
	if err != nil {
		log.Fatal("getfile err ", err)
	}
	defer f.Close()
	headimg := h.Filename
	// 验证后缀名
	ext := strings.ToLower(path.Ext(headimg))
	if ext != ".jpg" && ext != ".png" {
		//fmt.Println("验证后缀名格式错误！")
		log := models.UitlLog()
		log.Error("验证后缀名格式错误")
		c.Data["ext"] = "仅支持.jpg或.png格式的图片文件。"
		return
	}

	if name != "" && passwd != "" && sex != "" {
		num, err := models.ReadByName(name)
		if err != nil {
			log := models.UitlLog()
			log.Error("ReadByName failed", err.Error())
			return
		}
		if num > 0 {
			c.Ctx.SetCookie("register", "fail", 5)
			c.Ctx.Redirect(302, "/user")
			return
		}
		// orm insert
		id, err := models.Register(name, sex, passwd, headimg)
		if err != nil {
			log := models.UitlLog()
			log.Error("Register failed", err.Error())
			return
		}
		result := make(map[string]interface{})
		result["id"] = id
		result["msg"] = "OK"
		c.Ctx.SetCookie("register", "Succese", 5)
		c.Data["json"] = result
		c.Ctx.Redirect(302, "/login")
		//c.ServeJSON()
		//return

	}
	//c.ServeJSON()
	//
	return
	// 返回json 数据， 页面 通过js location 跳到 评论列表 //http://127.0.0.1:8080/main/getmessagelist

}

//获取用户列表
//http://127.0.0.1:8080/getuserlist?offset=0&limit=2
func (c *UserMainController) UserList() {
	c.TplName = "user_tb.html"
}
func (c *UserMainController) GetUserList() {

	// 读取数据库内容
	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 读取数据库
	result := make(map[string]interface{})
	result["count"] = 0
	//获取 offset 转成int 类型
	offsetStr := template.HTMLEscapeString(c.Input().Get("offset"))
	offset, err := strconv.Atoi(offsetStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("string 转int64 err", err.Error())
		return

	}
	//获取limit 转成int 类型
	limitStr := template.HTMLEscapeString(c.Input().Get("limit"))
	limit, err := strconv.Atoi(limitStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("string 转int64 err", err.Error())
		return
	}

	usertableList, err := models.GetUserList(offset, limit)
	if err != nil {
		result["msg"] = "get dataList failed" + err.Error()
		log := models.UitlLog()
		log.Error("GetUserList err", err.Error())
		return
	}
	var count = models.GetUserCount() //获取用户总数
	result["count"] = count
	result["data"] = usertableList
	c.Data["json"] = result
	c.ServeJSON()
}

//查询单个用户
//http://127.0.0.1:8080/getuser?id=1
func (c *UserMainController) GetOneUser() {
	idStr := template.HTMLEscapeString(c.Input().Get("id"))
	id, err := strconv.Atoi(idStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("string 转int64 err", err.Error())
		return
	}
	user := models.GetUserById(int64(id))
	uname := user.Name
	usex := user.Sex
	usta := strconv.Itoa(user.Status)
	uheadpic := user.Headpic
	result := make(map[string]interface{})
	result["id"] = idStr
	result["name"] = uname
	result["sex"] = usex
	result["sta"] = usta
	result["pic"] = uheadpic
	c.Data["json"] = result
	c.ServeJSON()
	//c.TplName = "up_user.html"
	return
}

//模糊查询用户
//http://127.0.0.1:8080//fuzzyuser?val=1
func (c *UserMainController) FuzzyUser() {
	val := template.HTMLEscapeString(c.Input().Get("val"))
	result := make(map[string]interface{})
	result["count"] = 0
	//获取 offset 转成int 类型
	offsetStr := template.HTMLEscapeString(c.Input().Get("offset"))
	offset, err := strconv.Atoi(offsetStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("string 转int64 err", err.Error())
		return

	}
	//获取limit 转成int 类型
	limitStr := template.HTMLEscapeString(c.Input().Get("limit"))
	limit, err := strconv.Atoi(limitStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("string 转int64 err", err.Error())
		return
	}

	if val != "" {
		data, err := models.UserFuzzyQs(val, offset, limit)
		if err != nil {
			log := models.UitlLog()
			log.Error("UserFuzzyQs err", err.Error())
			return
		}
		var count = models.UserFuzzyQsCount(val) //获取查询用户数
		result["count"] = count
		result["data"] = data
		c.Data["json"] = result
		c.ServeJSON()
		return
	}
}

//注销个人账户
//http://127.0.0.1:8080/deluser?id=3
func (c *UserMainController) DelUserById() {

	// 读取数据库内容
	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 读取数据库

	idStr := template.HTMLEscapeString(c.Input().Get("id"))
	id, err := strconv.Atoi(idStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("string 转int64 err", err.Error())
		return
	}
	result := make(map[string]interface{})
	//执行删除操作
	delNubmer, err := models.DelUser(int64(id))
	if err != nil {
		result["msg"] = "DelUser failed" + err.Error()
		log := models.UitlLog()
		log.Error("DelUser err", err.Error())
		return
	}
	// 成功返回数据
	result["count"] = delNubmer
	result["success"] = "ok"
	c.Data["json"] = result
	c.ServeJSON()
}

//更改用户信息
//http://127.0.0.1:8080/upuser?id=2&name=ii&sex=%E5%A5%B3&passwd=126
func (c *UserMainController) UpUserById() {

	// 读取修改数据库内容
	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 读取修改数据库

	idStr := template.HTMLEscapeString(c.Input().Get("id"))
	id, _ := strconv.Atoi(idStr)
	name := template.HTMLEscapeString(c.Input().Get("name"))
	sex := template.HTMLEscapeString(c.Input().Get("sex"))
	passwd := template.HTMLEscapeString(c.Input().Get("passwd"))
	staStr := template.HTMLEscapeString(c.Input().Get("sta"))
	sta, err := strconv.Atoi(staStr)

	if err != nil {
		log := models.UitlLog()
		log.Error("string 转int64 err", err.Error())
		return
	}
	// 获得图片name
	headimg := c.Input().Get("InputImgFile")
	url := "/user/getone?x=1&id=" + idStr
	result := make(map[string]interface{})
	if name != "" && passwd != "" && sex != "" && headimg != "" {
		num, err := models.ReadByName(name)
		if err != nil {
			log := models.UitlLog()
			log.Error("ReadByName err", err.Error())
			return
		}
		if num > 0 {
			c.Ctx.SetCookie("up", "fail", 5)
			c.Ctx.Redirect(302, url)
			return
		}
		// orm insert
		Upuser := models.UpUser(int64(id), int64(sta), name, sex, passwd, headimg)
		if Upuser.Userid > 0 {
			c.Data["suceese"] = "修改成功！"
		}
		result["user"] = Upuser
		c.Data["json"] = result
		//c.ServeJSON()
		//return
	}
	c.Ctx.Redirect(302, url)
	//c.TplName = "up_user.html"
	// 成功返回数据

	//result["msg"] = "ok"
	//
	//c.Data["json"] = result
	//c.ServeJSON()
}

//j

//却换禁言状态status
func (c *UserMainController) DoEditStatus() {
	idStr := template.HTMLEscapeString(c.Input().Get("id"))
	id, errc := strconv.Atoi(idStr)
	if errc != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + errc.Error())
		return
	}
	stuStr := template.HTMLEscapeString(c.Input().Get("num"))
	stu, errs := strconv.Atoi(stuStr)
	if errs != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + errs.Error())
		return
	}
	err := models.EditStatus(int64(id), int64(stu))
	if err != nil {
		log := models.UitlLog()
		log.Error("UpStaVal failed err" + err.Error())
		return
	}
	c.Data["json"] = "成功！"
	c.ServeJSON()
	return
}
